window.copyImageButtonModule = (function() {
    // 依赖容器
    const dependencies = {
        copyRowAsImage: null,
        showToast: null,
        loadHtml2Canvas: null
    };

    // 初始化方法
    function init(config) {
        Object.assign(dependencies, config);
    }

    // 核心功能
    function createCopyImageButton(row) {
        const btn = document.createElement('button');
        btn.className = 'copy-btn copy-image-btn';
        btn.innerHTML = '🖼️';
        btn.title = '复制行截图';
        btn.style.cssText = `
            background: none;
            border: none;
            cursor: pointer;
            font-size: 16px;
            padding: 4px;
            border-radius: 3px;
            background-color: rgba(0,0,0,0.05);
            transition: all 0.2s;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            width: 24px;
            height: 24px;
        `;

        btn.addEventListener('click', async function(event) {
            const button = event.currentTarget;
            const row = button.closest('[role="row"]');

            if (!row) {
                console.error('无法找到所在行');
                return;
            }

            try {
                // 加载html2canvas（如果需要）
                if (dependencies.loadHtml2Canvas) {
                    await dependencies.loadHtml2Canvas();
                }

                // 执行截图复制
                const result = await dependencies.copyRowAsImage(row);

                // 处理结果
                if (result.success) {
                    dependencies.showToast(`截图已复制`, 'success');
                } else {
                    dependencies.showToast('截图复制失败', 'info');
                }
            } catch (error) {
                console.error('截图失败:', error);
                dependencies.showToast('截图失败: ' + error.message, 'error');
            }
        });

        return btn;
    }

    return {
        init,
        createCopyImageButton
    };
})();
